package com.qys.livesMall.product.controller.sys;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.framework.utils.SecurityUtils;
import com.qys.livesMall.product.entity.H2ProductCategory;
import com.qys.livesMall.product.model.EditH2ProductCategoryDTO;
import com.qys.livesMall.product.service.H2ProductCategoryService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 *  商品分类
 * @author gmx
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/h2-shop-category")
public class H2ProductCategoryController {
    @Resource
    private H2ProductCategoryService h2ProductCategoryService;

    /****
     * 根据父ID查询子分类,默认0
     */
    @GetMapping(value = "/parent/{pid}")
    public R<List<H2ProductCategory>> list(@PathVariable(value = "pid")Long pid){
        List<H2ProductCategory> categories = h2ProductCategoryService.queryByParentId(pid);
        return R.ok(categories);
    }
    /****
     * 分页查询
     */
    @PostMapping(value = "/pageList")
    public R<IPage<H2ProductCategory>> List(@RequestBody Page<H2ProductCategory> page){
        QueryWrapper<H2ProductCategory> queryWrapper = new QueryWrapper<>();
        H2ProductCategory r = page.getRecords().get(0);
        queryWrapper.lambda().eq(ObjectUtils.isNotNull(r.getParentId()), H2ProductCategory::getParentId, r.getParentId())
                .eq(ObjectUtils.isNotNull(r.getDisplayName()), H2ProductCategory::getDisplayName,r.getDisplayName())
                .eq(ObjectUtils.isNotNull(r.getEnable()), H2ProductCategory::getEnable, r.getEnable())
                .eq(ObjectUtils.isNotNull(r.getLevel()), H2ProductCategory::getLevel, r.getLevel())
                .eq(ObjectUtils.isNotNull(r.getName()), H2ProductCategory::getName, r.getName())
                .eq(H2ProductCategory::getIsDelete,0);

        Page<H2ProductCategory> data = h2ProductCategoryService.page(page,queryWrapper);
        return R.ok(data);
    }
    /****
     * 添加分类或者修改
     */
    @PostMapping(value = "/save")
    public R save(@RequestBody EditH2ProductCategoryDTO dto){
        H2ProductCategory h2ProductCategory = new H2ProductCategory();
        BeanUtils.copyProperties(dto, h2ProductCategory);
        if (ObjectUtils.isEmpty(h2ProductCategory.getLevel())){
            return R.fail();
        }
        if (ObjectUtils.isEmpty(h2ProductCategory.getPostage())){
            h2ProductCategory.setPostage(BigDecimal.ZERO);
        }
        if (h2ProductCategory.getLevel().equals("1")){
            h2ProductCategory.setParentId(0L);
        }
        if (ObjectUtils.isEmpty(h2ProductCategory.getId())){
            h2ProductCategory.setCreateBy(SecurityUtils.getUsername());
            h2ProductCategory.setUpdateBy(SecurityUtils.getUsername());
            h2ProductCategory.setCreateTime(LocalDateTime.now());
            h2ProductCategory.setUpdateTime(LocalDateTime.now());
            boolean res = h2ProductCategoryService.save(h2ProductCategory);
            if (!res){
                return R.fail();
            }
        }else {
            h2ProductCategory.setUpdateBy(SecurityUtils.getUsername());
            h2ProductCategory.setUpdateTime(LocalDateTime.now());
            boolean res=  h2ProductCategoryService.updateById(h2ProductCategory);
            if (!res){
                return R.fail();
            }
        }
        return R.ok();
    }
    @Operation(summary = "启用开关")
    @GetMapping("/enable/{cid}")
    public R enable(@PathVariable Long cid){
        H2ProductCategory category = h2ProductCategoryService.getById(cid);
        if (ObjectUtils.isEmpty(category)){
            return R.fail();
        }
        if (category.getEnable().equals(1)) {
            category.setEnable(2);
        } else {
            category.setEnable(1);
        }
        return h2ProductCategoryService.updateById(category)?R.ok():R.fail();
    }
    /****
     * 批量删除
     */
    @DeleteMapping
    public R delete(@RequestBody List<H2ProductCategory> h2ShopCategories){
        return h2ProductCategoryService.deleteByids(h2ShopCategories);
    }

}
